package question

type ListNode struct {
	Val  int
	Next *ListNode
}

/*
1计算链表长度
2遍历获取正序位置元素
*/
func getKthFromEnd(head ListNode, k int) ListNode {
	num := 1
	firstNode := head
	for head.Next != nil {
		num++
		head = *head.Next
	}
	index := num - k
	for i := 0; i < num; i++ {
		if i == index {
			return firstNode
		} else {
			firstNode = *firstNode.Next
		}
	}
	return firstNode
}
